package com.sunshine.wrongquestionset.activity.login

import android.app.Application

import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import com.sunshine.wrongquestionset.R
import com.sunshine.wrongquestionset.room.entity.*

import com.sunshine.wrongquestionset.room.repository.MyRepository

/**
 * @author sunshine
 * @date 2021/3/27
 */
class LoginViewModel(application: Application): AndroidViewModel(application) {
    private val repository: MyRepository = MyRepository(application.applicationContext)
    private val myApplication: Application = application

    /**
     * @param userId 用户id
     * @return 用户实体类，可能为空
     */
    fun toLogin(userId: String): LiveData<UserEntity?> {
        return repository.getUser(userId)
    }

    /**
     * @param userId 用户id
     * @return 该账户是否注册过
     */
    fun findUser(userId: String): Boolean {
        return repository.findUser(userId)
    }

    /**
     * @param userEntity 用户实体
     */
    fun toRegister(userEntity: UserEntity) {
        repository.insertUser(userEntity)
        //创建一个默认科目
        //repository.insertSubjectByEntity(SubjectEntity(1, userEntity.id, "默认"))
        repository.insertSubject(userEntity.id, "默认")
//        //创建用户后创建一个默认错题集
//        repository.insertQuestionSet(userEntity.id, myApplication.getString(R.string.default_question_set), myApplication.getString(R.string.default_question_set_description))
        //创建默认提醒
        repository.insertQuestionTypeByEntity(QuestionTypeEntity(1, "选择"))
        repository.insertQuestionTypeByEntity(QuestionTypeEntity(2, "填空"))
        repository.insertQuestionTypeByEntity(QuestionTypeEntity(3, "判断"))
        repository.insertQuestionTypeByEntity(QuestionTypeEntity(4, "大题"))
        //现在，已经没有默认错题本这个说法，所以now为-1
        repository.insertAppConfig(AppConfigEntity(userEntity.id, false, -1))
    }

    /**
     * 添加一个用户的配置信息
     */
    fun addAppConfig(appConfigEntity: AppConfigEntity) {
        repository.insertAppConfig(appConfigEntity)
    }

    /**
     * 登录信息更新
     */
    fun updateAppConfig(userId: String, isLogin: Boolean) {
        repository.updateAppConfig(userId, isLogin)
    }

//    /**
//     * 获取默认的错题集
//     */
//    fun getDefaultQuestionSet(userId: String): LiveData<QuestionSetEntity> {
//        return repository.getDefaultQuestionSet(userId)
//    }

}